package org.example.demo21.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/file")
public class FileUpDownload {

    @Operation(summary = "上传", description = "上传", method = "POST")
    @PostMapping(value = "/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) {

        //父级目录
        String directory = "D:";
        //分隔符
        String separator = FileSystems.getDefault().getSeparator();

        //真实存储路径
        String url = directory + separator + "file" + separator + UUID.randomUUID();
        File f = new File(url);
        if (!f.exists() && !f.mkdirs()) {
            throw new RuntimeException("目录创建失败");
        }
        String name = file.getOriginalFilename();
        String realUrl = url + separator + name;
        File realPath = new File(realUrl);
        if (!realPath.getParentFile().exists() && !realPath.getParentFile().mkdir()) {
            throw new RuntimeException("目录创建失败");
        }
        if (realPath.exists()) {
            realPath.delete();
        }
        try {
            file.transferTo(realPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ret", 0);
        map.put("data", "ok");
        return map;
    }

    @GetMapping(value = "/download")
    @Operation(summary = "文件下载", description = "文件下载", method = "GET")
    public void download(HttpServletResponse response) throws Exception {

        FileInputStream in = null;
        OutputStream out = null;
        try {
            response.reset();
            response.setContentType("text/plain");
            response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode("test222", StandardCharsets.UTF_8));
            in = new FileInputStream("D:\\file\\cb42620c-c661-4d89-93bf-e0ad27964f83\\test22.txt");
            out = response.getOutputStream();
            byte[] b = new byte[1024 * 1024];
            int len;
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
